#include "widget_ai.h"
#include "players.h"
#include "gui.h"
#include "ai.h"
#include <sstream>
#include <iostream>
#include <map>
#include <string>



ai_widget::ai_widget( player* pl, QPushButton* nb, QWidget* parent, Qt::WindowFlags flags ): QWidget( parent, flags )
{
	next_b = nb;
	next_b->setEnabled( false );
	plr = pl;
	QVBoxLayout* layout = new QVBoxLayout( this );

	QLabel* title_l = new QLabel( this );
	title_l->setTextFormat( Qt::RichText );
	std::ostringstream sout;
	sout << "<font size=6>" << pl->get_name() << " :</font>";
	title_l->setText( sout.str().c_str() );

	layout->addWidget( title_l );

	QLabel* select_alg_l = new QLabel( this );
	select_alg_l->setTextFormat( Qt::RichText );
	select_alg_l->setText( "<b><font size=6>AI algorithm select</font></b>" );
	layout->addWidget( select_alg_l );
	
	QWidget* select_alg_w = new QWidget( this );
	QHBoxLayout* select_alg_w_l = new QHBoxLayout( select_alg_w );

	ai_alg_i = new QComboBox( select_alg_w );
	for( std::map< std::string, algorithm* >::iterator it = ai_algorithms::get_instance()->algorithm_list.begin(); it != ai_algorithms::get_instance()->algorithm_list.end(); ++it )
		ai_alg_i->addItem( (it->first).c_str() );
	select_alg_w_l->addWidget( ai_alg_i );

	QPushButton* select_alg_b = new QPushButton( "Set algorithm", select_alg_w );
	select_alg_w_l->addWidget( select_alg_b );
	connect( select_alg_b, SIGNAL( clicked() ), this, SLOT( select_algorithm() ) );

	select_alg_w->setLayout( select_alg_w_l );
	layout->addWidget( select_alg_w );


	this->setLayout( layout );
};



void ai_widget::select_algorithm()
{
	plr->ai_algorithm = ai_algorithms::get_instance()->algorithm_list[ ai_alg_i->currentText().toStdString() ]->clone();
	plr->ai_algorithm->init( plr );
	next_b->setEnabled( true );
};